package com.taojie.graduation.controller;

import com.alibaba.fastjson.JSON;
import com.taojie.graduation.domain.Category;
import com.taojie.graduation.service.api.CategoryService;
import com.taojie.graduation.utils.Result;
import com.taojie.graduation.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("category")
@Api(tags = {"类型接口"})
public class CategoryController {
    @Resource
    CategoryService categoryService;

    @ApiOperation("查找所有类型")
    @GetMapping("all")
    public Result getAll(){
        List<Object> all = categoryService.findAll();
        return new Result(ResultCode.SUCCESS, all);
    }

    @ApiOperation("添加类型")
    @PostMapping("add")
    public Result addCategory(@RequestBody Category category){
        return categoryService.addCategory(category);
    }

    @ApiOperation("删除")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int")
    @DeleteMapping("{id}")
    public Result deleteCategory(@PathVariable("id") Integer id){
        return categoryService.deleteById(id);
    }

    @ApiOperation("修改")
    @PutMapping("{id}")
    public Result updateById(@PathVariable("id") Integer id, @RequestBody Category category){
        category.setId(id);
        return categoryService.updateById(category);
    }

    @ApiOperation("根据id查")
    @GetMapping("{id}")
    public Result findById(@PathVariable("id") Integer id) {
        return categoryService.findById(id);
    }

    @ApiOperation("根据商品id查找商品类型")
    @GetMapping("prod/{id}")
    public Result findByProdId(@PathVariable("id") Integer id){
        return categoryService.findByProdId(id);
    }

    @ApiOperation("给商品添加类型")
    @PostMapping("pord/{id}")
    public Result findByProdId(@PathVariable("id") Integer id, @RequestParam("cates") String cates){
        List<Integer> ids = JSON.parseObject(cates, List.class);
        System.out.println(ids);
       return categoryService.addByProdId(id, ids);
    }

    @ApiOperation("给商品添加类型")
    @DeleteMapping("pord/{id}")
    public Result deleteByProdId(@PathVariable("id") Integer id){
        return categoryService.deleteByProdId(id);
    }

    @ApiOperation("给商品修改类型")
    @PutMapping("pord/{id}")
    public Result updateByProdId(@PathVariable("id") Integer id, @RequestParam("cates") String cates){
        List<Integer> ids = JSON.parseObject(cates, List.class);
        System.out.println(ids);
        return categoryService.updateByProdId(id, ids);
    }

}
